<?php

/***

LZRoster: A roster for World of Warcraft.
Copyright (C) 2007  Emmanuel Cron

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

***/

require_once dirname(__FILE__)."/Character.class.php";
require_once dirname(__FILE__)."/Sql.class.php";
require_once dirname(__FILE__)."/TradeskillContainer.class.php";

class CharacterManager
{	
	static private $instance;
	
	private $sql;
	private $tradeskill_container;
	
	private function __construct()
	{	
		$this->sql = Sql::GetInstance();
		$this->tradeskill_container = TradeskillContainer::GetInstance();
	}
	
	static function GetInstance()
	{
		if ( empty(self::$instance) )
		{
			self::$instance = new CharacterManager();
		}
		
		return self::$instance;
	}
	
	function DeleteCharacter($name)
	{
		if ( $this->sql->Query("
			DELETE FROM
				`cp_characters`
			WHERE
				`Name`='".$name."'
		") != true)
		{
			return false;
		}
		
		if ( $this->sql->Query("
			DELETE FROM
				`cp_dungeons`
			WHERE
				`CharacterName`='".$name."'
		") != true)
		{
			return false;
		}
		
		if ( $this->sql->Query("
			DELETE FROM
				`cp_professions`
			WHERE
				`CharacterName`='".$name."'
		") != true)
		{
			return false;
		}
		
		return true;
	}
	
	function DeleteCharacterTradeskills($name, $tradeskill_id)
	{
		$tradeskill = $this->tradeskill_container->GetTradeskillById($tradeskill_id);
		
		if ( $this->sql->Query("
			DELETE FROM
				`cp_professions`
			WHERE
				`CharacterName`='".$name."'
			AND
				`ProfessionName` IN ('".implode("','", $tradeskill->GetLocales())."')
		") != true)
		{
			return false;
		}
		
		return true;
	}
	
	function DeleteCharacterTradeskillSpecialization($name, $specialization_id)
	{
		if ( $this->sql->Query("
			DELETE FROM
				`cp_specializations`
			WHERE
				`CharacterName`='".$name."'
			AND
				`Specialization`='".$specialization_id."'
		") != true)
		{
			return false;
		}
		
		return true;
	}
	
	function GetCharactersAndProfessionsByAccountId($id)
	{		
		$characters_array = array();
		
		$this->sql->Query("
			SELECT DISTINCT
				`cp_characters`.*,
				GROUP_CONCAT(DISTINCT `cp_specializations`.`Specialization`) AS `Specializations`,
				GROUP_CONCAT(DISTINCT `cp_professions`.`ProfessionName`) AS `Professions`
			FROM
				`cp_characters`
			LEFT JOIN
				`cp_professions`
			ON
				`cp_professions`.`CharacterName`=`cp_characters`.`Name`
			LEFT JOIN
				`cp_specializations`
			ON
				`cp_specializations`.`CharacterName`=`cp_characters`.`Name`
			WHERE
				`cp_characters`.`character_account_id`='".$id."'
			GROUP BY
				`cp_characters`.`character_id`
			ORDER BY
				`cp_characters`.`Name`
		");
		
		while ( $v = $this->sql->IterateResults() )
		{
			$characters_array[$v['Name']] = new Character($v);
		}
		
		return $characters_array;
	}
	
	function GetAllCharacters( $character_sort_method )
	{
		$characters_array = array();
		
		$this->sql->Query("
			SELECT
				`cp_characters`.*,
				GROUP_CONCAT(`cp_specializations`.`Specialization`) AS `Specializations`
			FROM
				`cp_characters`
			LEFT JOIN
				`cp_specializations`
			ON
				`cp_specializations`.`CharacterName` = `cp_characters`.`Name`
			".$character_sort_method->GetGroupBySqlQuery()."
		");
		
		while ( $v = $this->sql->IterateResults() )
		{
			if ( $character_sort_method->IsDivided() )
			{
				if ( !array_key_exists($character_sort_method->GetSubSectionName($v), $characters_array))
				{
					$characters_array[$character_sort_method->GetSubSectionName($v)] = array();
				}
				
				array_push($characters_array[$character_sort_method->GetSubSectionName($v)], new Character($v));
			}
			else
			{
				array_push($characters_array, new Character($v));
			}
		}
		
		return $characters_array;
	}
	
	function GetAllCharactersWithDungeonsAccess()
	{
		$characters_array = array();
		
		$this->sql->Query("
			SELECT
				`cp_characters`.*,
				`cp_dungeons`.`Dungeon`
			FROM
				`cp_characters`
			LEFT JOIN
				`cp_dungeons`
			ON
				`cp_dungeons`.`CharacterName`=`cp_characters`.`Name`
			ORDER BY
				`cp_characters`.`Name`
		");
		
		while ( $v = $this->sql->IterateResults() )
		{
			if ( !array_key_exists($v['Name'], $characters_array) )
			{
				$characters_array[$v['Name']] = new Character($v);
			}
			else
			{
				$characters_array[$v['Name']]->AddDungeon($v['Dungeon']);
			}
		}
		
		return $characters_array;
	}
}

?>